import UpdateInvesterBuyType from '../../common/update-invester-buy-type/update-invester-buy-type.vue'
import UpdatePackageActivity from '../../common/update-package-activity/update-package-activity.vue'
import { swapSubtype, getInvesterBuyTypeList, setSubtypeStatus, deleteSubtype, showMsg } from '../../../../../vuex/action.js'
import extend from 'extend'

export default {
  name: 'PackageActivityItem',
  data() {
    return {
    }
  },
  props: ['subtypeList', 'detail', 'index'],
  methods: {
    deepClone(obj) {
      return extend(true, {}, obj)
    },
    moveUp(index) {
      let fromBetaId = this.subtypeList[this.index].id
      let toBetaId = this.subtypeList[this.index - 1].id
      this.swapInvesterbeta(fromBetaId, toBetaId)
    },
    moveDown(index) {
      let fromBetaId = this.subtypeList[index].id
      let toBetaId = this.subtypeList[index + 1].id
      this.swapInvesterbeta(fromBetaId, toBetaId)
    },
    evtSetStatus(detail) {
      // 上架需先检查套餐，下架可以直接下架
      if (detail.status === 1) {
        this.setStatus(detail)
      } else {
        this.setOnShelves(detail)
      }
    },
    setOnShelves(detail) {
      this.xGetInvesterBuyTypeList({
        id: detail.betaId,
        subType: detail.subType
      }).then((res) => {
        let buytypeList = res.data.list
        if (buytypeList && buytypeList.length > 0) {
          this.setStatus(detail)
        } else {
          this.xShowMsg({
            type: 'danger',
            content: '请先设置付费套餐'
          })
        }
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    setStatus(detail) {
      let params = {
        id: +detail.id,
        subType: detail.subType,
        status: +detail.status === 1 ? 0 : 1
      }
      this.xSetSubtypeStatus(params)
        .then((res) => {
          this.$dispatch('on_refresh_list')
        }).catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    deleteSubtype(detail) {
      // 删除组合直接传id，不传betaid
      this.xDeleteSubtype({
        id: detail.id
      }).then(() => {
        this.xShowMsg({
          type: 'info',
          content: '删除' + detail.name + '组合成功'
        })
        this.$dispatch('on_refresh_list')
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    swapInvesterbeta(fromBetaId, toBetaId) {
      this.xSwapSubtype({
        id1: fromBetaId,
        id2: toBetaId
      }).then((res) => {
        this.$dispatch('on_refresh_list')
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xSetSubtypeStatus: setSubtypeStatus,
      xDeleteSubtype: deleteSubtype,
      xSwapSubtype: swapSubtype,
      xGetInvesterBuyTypeList: getInvesterBuyTypeList
    },
    getters: {
      payType: state => state.payType
    }
  },
  components: {
    'update-invester-buy-type': UpdateInvesterBuyType,
    'update-package-activity': UpdatePackageActivity
  },
  ready() {
  }
}
